//
//  ViewController.m
//  LTOVECustom
//
//  Created by LTOVE on 2017/1/22.
//  Copyright © 2017年 LTOVE. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UIView *headerViews;
@property(nonatomic,strong)NSMutableArray *isCloose;
@property(nonatomic,strong)NSMutableArray *sectionTitle;
@end

@implementation ViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)sectionTitle{
    if (!_sectionTitle) {
        _sectionTitle = [NSMutableArray array];
    }
    return _sectionTitle;
}
- (UIView *)headerViews{
    if (!_headerViews) {
        _headerViews = [UIView new];
        _headerViews.backgroundColor = [UIColor grayColor];
    }
    return _headerViews;
}
- (NSMutableArray *)isCloose{
    if (_isCloose == nil) {
        _isCloose = [NSMutableArray array];
    }
    return _isCloose;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
    self.title = @"MyCustomClass";
    [self.tableView registerClass:[UITableViewCell  class] forCellReuseIdentifier:@"cell"];
    [self toInitDataArray];
    [self.tableView reloadData];
//    self.view.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.isCloose[section]  isEqual: @YES]) {
        return 0;
    }else{
        NSArray *arrays = self.dataArray[section];
        return arrays.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *arrays = self.dataArray[indexPath.section];
    cell.textLabel.text = arrays[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [self headerViewsWithTitle:self.sectionTitle[section] tag:section];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"aaaa";
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arrays = self.dataArray[indexPath.section];
    
        NSRange range = [arrays[indexPath.row] rangeOfString:@"."];
        NSString *className = [arrays[indexPath.row] substringFromIndex:range.location + 1];
        UIViewController *vc = [[NSClassFromString(className) alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)toInitDataArray{
    
    [self toAddOneGruopDataSourceWithTitle:@"UIKit" dataArray:@[
                                                                @"1.LTOVEButtionViewController",
                                                                @"2.LTOVECollectionViewNavController"
                                                                ]];
    
    
    for (int i =0 ; i < self.dataArray.count; i++) {
        [self.isCloose addObject:@YES];
    }
    NSLog(@"%@",self.isCloose);
}

- (void)toAddOneGruopDataSourceWithTitle:(NSString *)title dataArray:(NSArray *)array{
    [self.sectionTitle addObject:title];
    [self.dataArray addObject:array];
}

- (UIView *)headerViewsWithTitle:(NSString *)title tag:(NSInteger )tag{
    UIView *headerviewss = [UIView new];
    headerviewss.backgroundColor = [UIColor whiteColor];
    headerviewss.tag = tag;
    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerViewClick:)];
    [headerviewss addGestureRecognizer:gest];
    UILabel *label = [LTOVETools labelWithTitle:title font:[UIFont systemFontOfSize:12] textColor:[UIColor grayColor]];
    [label sizeToFit];
    [headerviewss addSubview:label];
    headerviewss.height = 30;
    headerviewss.width = [UIScreen mainScreen].bounds.size.width;
    label.centerY = headerviewss.centerY;
    label.x = 20;
    return headerviewss;
}
-(void)headerViewClick:(UIGestureRecognizer *)gest{
    NSLog(@"%ld",gest.view.tag);
    [self.isCloose replaceObjectAtIndex:gest.view.tag withObject:[self.isCloose[gest.view.tag] isEqual: @YES] ? @NO : @YES];
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:gest.view.tag] withRowAnimation:UITableViewRowAnimationNone];
}
@end
@interface ViewControllerModel : NSObject

@property(nonatomic,assign)BOOL isCloose;
@property(nonatomic,strong)NSArray *dataArray;
@end
@implementation ViewControllerModel

- (BOOL)isCloose{
    if (!_isCloose) {
        _isCloose = YES;
    }
    return _isCloose;
}

@end
